<?php
namespace app\api\controller;

use think\facade\Db;
use think\facade\Log;
use think\facade\Request;
use think\facade\Cache;

class AcceptZf
{
    private $redis;

    public function __construct()
    {
        $this->redis = Cache::store('redis')->handler();
    }

    public function notify_url()
    {
        $params = Request::param();
        //记录日志
        $time   = getUtcTime(false);
        Log::write("notify_({$time})-----------------------start\n");
        Log::write($params);
        Log::write("--------------\n");
        if(!is_array($params)){
            $params = json_decode($params , true);
        }
        $out_trade_no = $params['out_trade_no'];
        $amount       = $params['total_amount'];
        $list = Db::name('user_pay')->where([
            'out_trade_no'  =>$out_trade_no,
            'amount_en'     =>$amount,
            'status_check'  =>0
        ])->find();
        if(empty($list)){
            Log::write("Error: not find out_trade_no Or status_check=1|2 \n");
            Log::write("notify_({$time})-------------------------end\n");
            exit;
        }
        $id             = $list['id'];
        $user_id        = $list['user_id'];
        $amount         = $list['amount'];
        $ctime          = $list['ctime'];
        $buyer_logon_id = $params['buyer_logon_id'];
        unset($params['sign']);
        $trade_status   = $params['trade_status'];
        $time           = getUtcTime();
        if($trade_status == 'TRADE_SUCCESS'){
            Db::name('user_pay')->where(['id'=>$id])->update([
                'status_check'  =>1,
                'notify_desc'   =>json_encode($params),
                'pay_user'      =>$buyer_logon_id,
                'utime'         =>$time
            ]);
            Db::startTrans();
            try{
                $user_list      = Db::name('user')->find($user_id);
                $user_balance   = $user_list['balance'];
                $result_balance = $user_balance+$amount;
                Db::name('user_cash_log')->save([
                    'user_id'       => $user_id,
                    'type'          => 0, //1=debit
                    'action_type'   => 1, //3=消费
                    'amount'        => $amount,
                    'balance'       => $result_balance, //如果是进行修复的话，这里的余额要重新计算
                    'desc'          => "支付宝充值(扫码)",
                    'remark'        => $out_trade_no,
                    'ctime'         => $time
                ]);
                $check_num  = 'A' . getRandNumber();
                Db::name('fund_check')->save([
                    'user_id'       => $user_id,
                    'check_num'     => $check_num,
                    'amount'        => $amount,
                    'balance'       => $result_balance,
                    'ctime'         => $ctime,
                    'utime'         => $time,
                    'check_status'  => 1,
                    'pay_type'      => 3,
                    'trn'           => $out_trade_no,
                    'desc'          => "支付宝充值(扫码)",
                ]);
                Db::name('user')->where(['id'=>$user_id])->update(['balance'=>$result_balance]);
                Db::name('user_pay')->where(['id'=>$id])->update([
                    'status_used'  =>1   //数据已录入
                ]);
                Db::commit();
            } catch (\Exception $e) {
                $error_msg  = $e->getMessage();
                Db::name('api_error')->save([
                    'user_id'       => $user_id,
                    'amount'        => $amount,
                    'amount_bank'   => $amount,
                    'type'          => 'alipay_add',
                    'data_param'    => $error_msg,
                    'ctime'         => $time
                ]);
                Db::rollback();
            }
        }else{
            Db::name('user_pay')->where(['id'=>$id])->update([
                'status_check'  =>2,
                'notify_desc'   =>json_encode($params),
                'utime'         =>$time
            ]);
        }

    }
}
